package cn.ecnu.mapper.forum;

import cn.ecnu.pojo.forum.Post;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PostMapper {

    int insertPost(Post post); // 插入一个帖子

    @Delete("delete from pinke.post where post_id = #{postId}")
    int deletePostById(@Param("postId") Integer postId); // 删除一个帖子

    int updatePostById(Post post); // 更新一个帖子

    @Select("select * from pinke.post where post_id = #{postId};")
    Post getPostById(@Param("postId") Integer postId); // 根据id获取帖子

    @Select("select * from pinke.post where forum_id = #{forumId}")
    List<Post> getPostByForumId(@Param("forumId") Integer forumId);

    @Select("select * from pinke.post where user_id = #{userId}")
    List<Post> getPostByUserId(Integer userId);

    // 使用分页的方式获取Post
    @Select("select * from pinke.post where forum_id = #{forumId} limit #{startNum}, #{pageSize}")
    List<Post> getPostByForumIdUsePage(@Param("forumId") Integer forumId, @Param("startNum") Integer startNum, @Param("pageSize") Integer pageSize);

    @Select("select * from pinke.post where user_id = #{userId} limit #{startNum}, #{pageSize}")
    List<Post> getPostByUserIdUserPage(@Param("forumId") Integer forumId, @Param("startNum") Integer startNum, @Param("pageSize") Integer pageSize);

    // 按照时间或者热度排序
    @Select("select * from pinke.post where forum_id = #{forumId} order by post_time DESC limit #{startNum}, #{pageSize}")
    List<Post> getPostByForumIdUsePageOrderByTime(@Param("forumId") Integer forumId, @Param("startNum") Integer startNum, @Param("pageSize") Integer pageSize);

    @Select("select * from pinke.post where forum_id = #{forumId} order by hot DESC limit #{startNum}, #{pageSize}")
    List<Post> getPostByForumIdUsePageOrderByHot(@Param("forumId") Integer forumId, @Param("startNum") Integer startNum, @Param("pageSize") Integer pageSize);

    @Select("select * from pinke.post where user_id = #{userId} order by post_time DESC limit #{startNum}, #{pageSize}")
    List<Post> getPostByUserIdUsePageOrderByTime(@Param("userId") Integer forumId, @Param("startNum") Integer startNum, @Param("pageSize") Integer pageSize);

    @Select("select * from pinke.post where user_id = #{userId} order by hot DESC limit #{startNum}, #{pageSize}")
    List<Post> getPostByUserIdUsePageOrderByHot(@Param("userId") Integer forumId, @Param("startNum") Integer startNum, @Param("pageSize") Integer pageSize);

    @Select("select * from pinke.post where tag like concat('%', #{keyword}, '%') or post_content like concat('%', #{keyword}, '%')")
    List<Post> getPostByTagsAndContent(@Param("keyword") String keyword);


}